﻿using DataProcess.Interface;
using DataProcess.Interface.Models;
using System;
using System.Xml;

namespace DataProcessing.ShimadzuXRF
{
    public class DataProcessing : IDataProcessing
    {
        public string Name { get { return "Shimadzu(岛津) 荧光仪"; } }

        public IDataOutput Processing(string content)
        {
            XRayOutputModel outputModel = new XRayOutputModel();
            XmlDocument xmlDoc = new XmlDocument();            
            xmlDoc.LoadXml(content);            
            XmlNode xn = xmlDoc.SelectSingleNode("PCXRF_MULTI_FORMAT/PCXRF_USER_FORMAT");
            outputModel.SampleName = xn.SelectSingleNode("FileInfo/SampleName").InnerText;

            var measDateStr = xn.SelectSingleNode("FileInfo/MeasDateStr").InnerText;

            if (DateTime.TryParseExact(measDateStr, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime date))
            {
                outputModel.MeasureTime = date;
            }
            else
            {
                outputModel.MeasureTime = DateTime.Now;
            }
            XmlNodeList xnlAver = xn.SelectNodes("ResQuan_Aver/ResQuan_Aver_Chan");
            foreach (XmlNode xml in xnlAver)
            {
                XmlElement xe = (XmlElement)xml;
                var CompName = xe.SelectSingleNode("CompName").InnerText.TrimStart().TrimEnd();
                var Conc = xe.SelectSingleNode("Conc").InnerText.TrimStart().TrimEnd();
                if (!outputModel.Eelements.ContainsKey(CompName))
                {
                    outputModel.Eelements.Add(CompName, Conc);
                }
            }
            return outputModel;
        }
    }
}
